package com.hlkj.pay.app.merchant.ext.impl.helipay.util;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;

import java.nio.charset.StandardCharsets;

public class Des3 {

    public static String encode(String key, String data) {
        try {
            byte[] keyByte = key.getBytes(StandardCharsets.UTF_8);
            byte[] dataByte = data.getBytes(StandardCharsets.UTF_8);
            byte[] valueByte = MessageAuthenticationCode.des3Encryption(keyByte, dataByte);
            String value = Base64Encoder.encode(valueByte);
            return value;
        } catch (Exception e) {
            throw new IllegalStateException("加密失败了", e);
        }
    }

    public static String decode(String key, String value) {
        try {
            byte[] keyByte = key.getBytes(StandardCharsets.UTF_8);
            byte[] valueByte = Base64Decoder.decode(value.getBytes(StandardCharsets.UTF_8));
            byte[] dataByte = MessageAuthenticationCode.des3Decryption(keyByte, valueByte);
            return new String(dataByte, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new IllegalStateException("解密失败了", e);
        }
    }


}
